<?php
namespace App\Services;
use App\Models\CourseContentModel;
use App\Models\CourseModel;

class CourseContentService{
    /**
     * 获取免费课程信息
     * @param int $courseid
     * @return object $info
     */
    public function getCourseContentInfoByFree($courseid){
        $info = CourseContentModel::orderBy('id','asc')->where('curriculum_id',$courseid)->where('is_free',0)
            ->where('isshow',1)->first();
        if(empty($info)){
            return false;
        }else{
            return $info;
        }
    }

	/**
	 * 插入课程信息
	 * @param array $data
	 * @return bool $res
	 */
	public function insertContent($data){
		$res = CourseContentModel::insert($data);
		return $res;
	}

	/**
	 * 获取课程列表
	 * @param int $courseid
	 * @param int $pagesize
	 * @param string $keywords
	 * @return object $contentlist
	 */
	public function getCourseContentList($pagesize,$courseid=0){
		$contentlist = CourseContentModel::orderBy('id','asc')
				->where('isshow',1)
				->where(function($query) use ($courseid){
					if($courseid > 0){
						$query->where('curriculum_id',$courseid);
					}
				})
				->paginate($pagesize);

		foreach($contentlist as $ctm=>$cst){
			$info = CourseModel::where('id',$cst->curriculum_id)->select('name')->first();
			if(!empty($info)){
				$contentlist[$ctm]->coursename = $info->name;
			}else{
				$contentlist[$ctm]->coursename = '';
			}
		}
		
		return $contentlist;	
	}

	/**
	 * 获取课程视频信息
	 * @param int $id
	 * @return object $contentinfo
	 */
	public function getCourseContentInfoById($id){
		$contentinfo = CourseContentModel::where('id',$id)->first();
		return $contentinfo;
	}

	/**
	 * 获取课程视频信息总数
	 * @param int $courseid
	 * @return int $num
	 */
	public function getCourseContentNum($courseid){
		$num = CourseContentModel::where('curriculum_id',$courseid)->count();
		return $num;
	}

	/**
	 * 更新视频信息
	 * @param array $data
	 * @param int $id
	 * @return bool $res
	 */
	public function modifyCoueseContent($data,$id){
		$res = CourseContentModel::where('id',$id)->update($data);
		return $res;
	}


    // 获取最新的视频信息
    public function getVideoContent($courseid){
        $content = CourseContentModel::orderBy('id','desc')->where('curriculum_id',$courseid)->first();
        if(!empty($content)){
            $title = $content->video_name;

            $arr = array('第','集');
            $title = str_replace($arr,'',$title);
            return $title;
        }else{
            return false;
        }

    }
}